﻿/// <reference path="../../jQuery/jquery.js"/>
/// <reference path="../../jQuery/jquery-calendar.js"/>
/// <reference path="../../jQuery/jquery.autocomplete.js"/>
/// <reference path="../../jQuery/jquery.autoloadselect.js"/>
/// <reference path="../../jQuery/jquery.wizard.js"/>

var j=jQuery.noConflict();

jQuery(function ()
{
	(function ($)
	{
		$('#BioTabs').tabs();

		if ($('#photoDiv img').length > 0)
			$("#RemovePic").enable();

		$("#RemovePic").click(function (event)
		{
			j.getJSON(siteRoot + '/Admin/RemovePhoto.rails',
			  { id: personId },
			  function (body, status)
			  {
				$('#photoDiv').text('** No Image **');
				$("#RemovePic").disable();
			  });
		});

		$("#SendPic").click(function (event)
		{
			$("#PhotoForm")
			.ajaxSubmit({ url: siteRoot + '/Admin/UploadPhoto.rails', type: 'post', clearForm: true, dataType: 'json',
				success: function (body, status, set)
				{
					if (body.status === "OK")
					{
						$('#photoDiv').html($("<img />").attr("src", siteRoot + "/content/images/upload/" + body.filename + "?a=" + new Date().getTime()));
						$("#RemovePic").enable();
					}
					else
						$("#divNotice").text("Error:  " + body.Message);

				}
			});
			$('#photoDiv').html($("<img />").attr("src", siteRoot + "/content/css/jQuery/ui/images/loading.gif"));
		});

		$("#SaveText").click(function (event)
		{
			$("#newBio_Bio").val($("#pupedit-text").val());
			$("#BioForm")
			.ajaxSubmit({ dataType: 'json',
				success: function (body, status, set)
				{
					switch (body.status)
					{
						case 'PENDING':
							$("#divNotice").text("Your changes are saved, pending approval.  They will be online shortly.  ");
							break;

						case 'APPROVED':
							$("#divNotice").text("Your changes are saved. They should be viewable immediately.  ");
							break;
						case 'BAD':
							$("#divNotice").text("Your changes are saved. Error Sending Email.  ");
							break;
					}
					$("input:button").disable();
					$("textarea").disable();
					$("#divNotice").append($("<span>Click <b><a href='" + siteRoot + "/Person/display.rails?id=" + personId + "'>here</a></b> to return to bio page.</span>"));

				}  // end success function
			});   // end ajaxSubmit
			$("#divNotice").text("One moment as your changes are being saved.");
			$('#divNotice').append($("<img />").attr("src", siteRoot + "/content/css/jQuery/ui/images/loading.gif"));
		});  // end SaveText click

		$("#pupedit-text").maxlength({ feedback: '.charsLeft', length: 8000 });
	})(jQuery);
});